package config

import (
    "com.wxp.trade/model"
    "fmt"
    "gopkg.in/yaml.v2"
    "os"
    "path/filepath"
)

var Config model.Config

func MustInit() {
    ex, err := os.Executable()
    if err != nil {
        panic(err)
    }

    appPath := filepath.Dir(filepath.Dir(ex))

    env := os.Getenv("APP_ENV")
    file := fmt.Sprintf("%s/conf/config.%s.yaml", appPath, env)
    if env == "" {
        file = fmt.Sprintf("%s/conf/config.yaml", appPath)
    }

    _, err = os.Stat(file)
    if err != nil {
        panic(fmt.Sprintf("找不到对应环境[%s]的配置文件:%s", env, err))
    }

    b, err := os.ReadFile(file)
    if err != nil {
        panic(fmt.Sprintf("打开文件[%s]报错:%s", file, err))
    }

    err = yaml.Unmarshal(b, &Config)
    if err != nil {
        panic(fmt.Sprintf("解析文件[%s]报错:%s", file, err))
    }

    // 初始化DB
    MustInitDB()
}
